<?php
/**
 * Created by PhpStorm.
 * User: 10267
 * Date: 2014/12/4
 * Time: 15:21
 */
class PubStat{
    /**
     * 为二维数组中的其中一列计算百分比
     * @param $array
     * @param $fields array("User"=>array("sum"=>123,"to"=>"UserRate"))
     * @param $all array params of PubStat::getTotal($data,$params)).
     */
    public static function setRate(&$array,$fields = array(),$all = array(),&$total = array(),$html = false){
        if(!empty($all)){
            $total = self::getTotal($array,$all);
        }

        foreach($array as &$item){
            foreach($fields as $k => $v){
                $sum = PubFunc::defaultValue($v['sum'],$total[$k]);
                if($html){
                    $item[$v['to']] = $item[$k] . "(<span style='color:green;'>".LibMath::getPercent($item[$k] , $sum)."%</span>)";
                }else{
                    $item[$v['to']] = LibMath::getPercent($item[$k] , $sum) . "%";
                }

            }
        }

        unset($item);
    }

    /**
     * 小计
     * @param $data
     * @param $params array("total"=>array("RecDate"=>"Total"),"percentStyle"=>"color","sumKey"=>array("A","B","C"),"avgKey"=>array("D"=>"int","E"),"percentKey"=>array("F"=>array("dividend"=>"A","divisor"=>"B")));
     * @return mixed
     */
    public static function getTotal($data,$params){
        $totalKey = $params['sumKey'];
        $percentKey = $params['percentKey'];
        $avgKey = $params['avgKey'];
        $count = 0;
        $total = is_array($params['total']) ? $params['total'] : array();
        foreach($data as $item){
            $count++;
            foreach($totalKey as $key){
                $total[$key] += $item[$key];
            }
        }
        foreach($avgKey as $ak => $format){
            $total[$ak] = round($total[$ak]/$count,2);
            if($format == 'int'){
                $total[$ak] = intval($total[$ak]);
            }
        }
        foreach($percentKey as $k => $percent){
            $total[$k] = LibMath::getPercent($total[$percent['dividend']],$total[$percent['divisor']],4);
            if(strpos($params['percentStyle'],'color') !== false){
                $total[$k] = "<font color='green' class='reTitle'>".$total[$k]."</font>";
            }
            if(strpos($params['percentStyle'],'exp') !== false){
                $total[$k] = $total[$k] . ' ('.$total[$percent['dividend']] . '/' . $total[$percent['divisor']]. ')';
            }
        }
        return $total;
    }

    public static function getTop($data,$top = 20){
        $i = 0;
        foreach($data as $k => $item){
            if( $i++ > $top){
                unset($data[$k]);
            }
        }
        return $data;
    }

    public static function max($value1,$value2){
        return $value1 >= $value2 ? $value1 : $value2;
    }
    public static function min($value1,$value2){
        return $value1 <= $value2 ? $value1 : $value2;
    }

    public static function getMaxPercent($data,$key){
        $max = 0;
        foreach($data as $item){
            $test = floatval(str_replace("%","",$item[$key]));
            $max = self::max($max,$test);
        }
        return $max;
    }

    public static function format($data,$format){
        if(!$format){
            return $data;
        }
        foreach($data as &$item){
            //TODO add support of multi formatter
            foreach($format as $k => $formatter){
                if(!$item[$k]){
                    $item[$k] = "";
                }else{
                    $item[$k] = date("Y-m-d H:i:s",$item[$k]);
                }

            }
        }
        unset($item);
        return $data;
    }
}